//
//  ChangeBackgroundView.swift
//  loveHHEveryDay
//
//  Created by yw on 2025/4/18.
//

import SwiftUI

struct ChangeBackgroundView: View {
    @EnvironmentObject var pathManager:PathManager
    @AppStorage("user_bg") var bg:String = ""
    
    @State var list:[BackgroundModel] = [
        BackgroundModel(name: "橙子", value: "change_bg_1"),
        BackgroundModel(name: "樱桃", value: "change_bg_2"),
        BackgroundModel(name: "西瓜", value: "change_bg_3"),
        BackgroundModel(name: "葡萄", value: "change_bg_4"),
        BackgroundModel(name: "奇异果", value: "change_bg_5"),
        BackgroundModel(name: "苹果", value: "change_bg_6"),
        BackgroundModel(name: "柠檬", value: "change_bg_7"),
    ]
    
    @State var gridItems:[GridItem] = [
        GridItem(.fixed(110), spacing: 16, alignment: .center),
        GridItem(.fixed(110), spacing: 16, alignment: .center),
        GridItem(.fixed(110), spacing: 16, alignment: .center),
    ]
    
    @State var currentBg:String = ""
    
    var body: some View {
        VStack {
            ScrollView(showsIndicators: false) {
                LazyVGrid(columns: gridItems,spacing: 20) {
                    ForEach(list){ item in
                        Image(item.value)
                            .resizable()
                            .aspectRatio(contentMode: .fill)
                            .frame(width: kScreenWidth / 4)
                            .cornerRadius(10)
                            .scaleEffect(currentBg == item.value ? 1.1:1)
                            .shadow(radius:currentBg == item.value ? 4 : 0)
                            .shakeTapGes {
                                withAnimation {
                                    currentBg = item.value
                                   
                                }
                            }
                    }
                }.padding(.top,20)
                
                ShakeButton(label: Text("确 定"), textColor: .colorTextMain, fontSize: 16, fontWeight: .bold) {
                    confirmBtnClick()
                }.padding(.top,40)
            }
        }
        .navigationTitle("修改背景")
//        .navigationBarTitleDisplayMode(.inline)
        .frame(maxWidth: .infinity)
        .background(Color("color_bg"))
        .navigationBack()
    }
    
    private func confirmBtnClick() {
        bg = currentBg
        YWToast.show(text: "修改成功")
        DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
            pathManager.path.removeLast()
        }
       
    }
}


